package com.qs.worklog.util;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;

public class DataSourceUtil {

	private static DataSource ds;
	private static QueryRunner qr;
	
	public static DataSource getDatsSource(){
		if(ds == null){
			ds = new DataSource();
		}
		return ds;
	}
	
	public static QueryRunner getQueryRunner(){
		if(qr == null){
			qr = new QueryRunner(getDatsSource());
		}
		return qr;
	}
	
}

class DataSource implements javax.sql.DataSource{
	private PrintWriter printWrite;
	private int loginTinmeout;
	private Connection conn;
	
	public DataSource(){
		try {
			loginTinmeout = 200;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public PrintWriter getLogWriter() throws SQLException {
		return printWrite;
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		this.printWrite = out;
	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		this.loginTinmeout = seconds;
	}

	@Override
	public int getLoginTimeout() throws SQLException {
		return this.loginTinmeout;
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Connection getConnection() throws SQLException {
		conn = ConnectionUtil.getConnection();
		return this.conn;
	}

	@Override
	public Connection getConnection(String username, String password) throws SQLException {
		return this.conn;
	}
	
}
